<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传与任务结果查询</title>
    <style>
        body {
            background-image: url('/static/imgs/bg.png'); /* 替换为你的图片路径 */
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        h1 {
            color: #333;
            margin-bottom: 10px;
        }

        form {
            background: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
            width: 200%; /* Ensure the form takes full width */
            max-width: 500px; /* Set a maximum width for the form */
        }


        input[type="file"],
        input[type="text"] {
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: calc(100% - 22px);
            margin-bottom: 10px;
            transition: border-color 0.3s;
        }

        input[type="file"]:hover,
        input[type="text"]:hover {
            border-color: #66afe9;
        }

        button {
            background-color: #5cb85c;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
            width: 100%; /* Button takes full width */
        }

        button:hover {
            background-color: #4cae4c;
        }

        #response, #taskResponse {
            margin-top: 10px;
            padding: 20px;
            padding-right: 70px;
            background: #e9ecef;
            border-left: 5px solid #007bff;
            border-radius: 5px;
            height: 8px;
            width: 100%; /* Response takes full width */
            max-width: 400px; /* Set a maximum width for the response */
            margin-bottom: 10px;
            overflow: hidden; /* 隐藏溢出文本 */
            text-overflow: ellipsis; /* 省略号处理（适用于单行文本） */
            white-space: nowrap; /* 不换行 */
        }


        hr {
            margin: 20px 0;
            border: 1px solid black;
            width: 144%; /* Make hr take full width */
            max-width: 1000px;

        }

        .form-container {
            width: 370px; /* 设置固定宽度 */
            height: 620px; /* 设置固定高度 */
            border: 2px solid black; /* 黑框 */
            padding: 20px; /* 内边距 */
            padding-right: 190px;
            border-radius: 5px; /* 圆角 */
            background: rgba(255, 255, 255, 0.8);
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* 阴影效果 */
        }


    </style>
</head>
<body>

<div class="form-container">
    <form id="uploadForm">
        <h1>上传文件</h1>
        <input type="file" id="fileInput" required>
        <button type="submit">上传</button>
    </form>
    <form>
        <div id="response"></div>
        <button id="copyButton">复制内容</button>

    </form>
    <hr>
    <form>
        <h1>任务结果下载</h1>
        <input type="text" id="taskIdInput" placeholder="输入任务ID" class="centered-input">
        <button id="queryButton" class="centered-button">下载</button>
        <div id="result"></div>
    </form>
</div>

<script>
    // 文件上传功能
    document.getElementById('uploadForm').addEventListener('submit', async function (event) {
        event.preventDefault();

        const file = document.getElementById('fileInput').files[0];
        if (!file) return;

        const formData = new FormData();
        formData.append('file', file);

        try {
            const response = await fetch('/upload', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                throw new Error(`HTTP错误: ${response.status}`);
            }

            const text = await response.text();
            document.getElementById('response').innerText = text;
            document.getElementById('fileInput').value = '';
        } catch (error) {
            document.getElementById('response').innerText = '上传失败: ' + error.message;
        }
    });

    // 查询任务并自动下载文件

    document.getElementById('queryButton').addEventListener('click', function () {
        const taskId = document.getElementById('taskIdInput').value.trim();
        if (!taskId) {
            alert('请先输入任务ID！');
            return;
        }

        const url = `/result?task_id=${taskId}`;
        const a = document.createElement('a');
        a.href = url;
        a.download = '';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);

    });


    document.getElementById('queryButton').addEventListener('click', () => {
        requestAnimationFrame(() => {
            document.getElementById('taskIdInput').value = '';
        });
    });

    document.getElementById('copyButton').addEventListener('click', () => {
        requestAnimationFrame(() => {
            document.getElementById('response').value = '';
        });
    });


    document.getElementById('copyButton').addEventListener('click', function () {

            const responseContent = document.getElementById('response').innerText;


            if (responseContent === "") {
                alert('没有内容可以复制！');
                return; // 结束函数，不进行复制
            }

            // 创建一个临时的文本区域
            const tempTextArea = document.createElement('textarea');
            tempTextArea.value = responseContent;
            document.body.appendChild(tempTextArea);

            // 选中文本
            tempTextArea.select();
            tempTextArea.setSelectionRange(0, 99999); // 对于手机设备

            // 复制文本
            document.execCommand('copy');

            // 移除临时文本区域
            document.body.removeChild(tempTextArea);


        }
    );


</script>
</body>
</html>
